package org.aisino.shdzfp.vistool.swing;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.aisino.shdzfp.vistool.util.PassWordCreate;
import org.apache.log4j.Logger;

public class PasswordCreatePanel extends JPanel {

	private final static Logger log = Logger.getLogger(PasswordCreatePanel.class);

	private char[] DICTIONARY;
	private static final int CAPACITY = 10 + 26 * 2;
	private JButton buttonGenerate;
	private JButton buttonReset;
	private JButton buttonAutoRandom;
	private JButton buttonCopy;
	private JTextField textRegistrationCode;
	private JTextField textRandomKey;
	private JTextField textPassword;
	private JLabel labelRegistrationCodeInfo;
	private JLabel labelRandomKeyInfo;
	private JTextField[] fields;
	private JLabel[] labels;
	
	public PasswordCreatePanel() {
		initDictory();
		paint();
		otherInit();
		setMonitor();
	}
	
	private void otherInit() {
		fields = new JTextField[3];
		fields[0] = textRegistrationCode;
		fields[1] = textRandomKey;
		fields[2] = textPassword;
		
		labels = new JLabel[2];
		labels[0] = labelRegistrationCodeInfo;
		labels[1] = labelRandomKeyInfo;
	}
	
	private void initDictory() {
		DICTIONARY = new char[CAPACITY];
		int curr = 0;
		for(int i = 48; i < 58; i++, curr++) {
			DICTIONARY[curr] = (char) i;
		}
		for(int i = 65; i < 91; i++, curr++) {
			DICTIONARY[curr] = (char) i;
		}
		for(int i = 97; i< 123; i++, curr++) {
			DICTIONARY[curr] = (char) i;
		}
	}

	private void paint() {
		this.setLayout(null);

		int margin = 10;
		
		int marginX = 180;
		int marginY = 100;
		
		//module a
		int labelX = 100;
		int moduleY = 30;
		int labelY = moduleY;
		int textX = 225;
		int textY = moduleY;
		int currX = marginX;
		int currY = marginY;
		int button1X = 40;
		int button1Y = moduleY;
		JLabel labelZcm = new JLabel("RegistrationCode:");
		textRegistrationCode = new JTextField(15);
		labelZcm.setBounds(currX = marginX, currY, labelX, labelY);
		textRegistrationCode.setBounds(currX += labelX, currY, textX - 50, textY);
		labelZcm.add(textRegistrationCode);
		labelRegistrationCodeInfo = new JLabel();
		labelRegistrationCodeInfo.setForeground(Color.red);
		labelRegistrationCodeInfo.setBounds(currX += (textX - 50 + button1X + 2 * margin), currY, labelX, labelY);
		add(labelZcm);
		add(textRegistrationCode);
		add(labelRegistrationCodeInfo);

		//module b
		currY += (moduleY + margin);
		JLabel labelRandomKey = new JLabel("RandomKey:");
		textRandomKey = new JTextField(15);
		labelRandomKey.add(textRandomKey);
		labelRandomKey.setBounds(currX = marginX, currY, labelX, labelY);
		textRandomKey.setBounds(currX += labelX, currY, textX - 50, textY);
		buttonAutoRandom = new JButton("R");
		buttonAutoRandom.setBounds(currX += (textX - 50 + margin), currY, button1X, button1Y);
		labelRandomKeyInfo = new JLabel();
		labelRandomKeyInfo.setForeground(Color.red);
		labelRandomKeyInfo.setBounds(currX += (button1X + margin), currY, labelX, labelY);
		add(labelRandomKey);
		add(textRandomKey);
		add(buttonAutoRandom);
		add(labelRandomKeyInfo);
		
		//module c
		currY += (moduleY + margin);
		JLabel labelPassword = new JLabel("Password:");
		textPassword = new JTextField(15);
		textPassword.setEditable(false);
		textPassword.setBackground(Color.gray);
		labelPassword.add(textPassword);
		labelPassword.setBounds(currX = marginX, currY, labelX, labelY);
		textPassword.setBounds(currX += labelX, currY, textX - 50, textY);
		add(labelPassword);
		add(textPassword);
		
		buttonCopy = new JButton("C");
		buttonCopy.setBounds(currX += (textX - 50 + margin), currY, button1X, button1Y);
		add(buttonCopy);
		
		//module d
		int button2X = 152;
		int button2Y = moduleY;
		int buttonMargin = 20;
		currY += (moduleY + margin);
		buttonGenerate = new JButton("Generate");
		buttonReset = new JButton("Reset");
		buttonGenerate.setBounds(currX = marginX, currY, button2X, button2Y);
		buttonReset.setBounds(currX += (button2X + buttonMargin), currY, button2X, button2Y);
		add(buttonGenerate);
		add(buttonReset);
		
	}

	private void setMonitor() {
		
		buttonAutoRandom.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				cleanInfo();
				textRandomKey.setText(getRandom());
			}
		});
		
		buttonGenerate.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					cleanInfo();
					if(check()) {
						textPassword.setText(PassWordCreate.passWordCreate(textRegistrationCode.getText(), textRandomKey.getText()));
					}
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		
		buttonReset.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				cleanText();
				cleanInfo();
			}
		});
		
		buttonCopy.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				StringSelection stsel = new StringSelection(textPassword.getText());
				Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);
			}
		});
	}
	
	private String getRandom() {
		String random = "";
		for(int i = 0; i < 10; i++) {
			random += DICTIONARY[new Random().nextInt(CAPACITY)];
		}
		return random;
	}
	
	private boolean check() {
		boolean result = true;
		String strRegistrationCode = textRegistrationCode.getText().trim();
		if("".equals(strRegistrationCode)) {
			labelRegistrationCodeInfo.setText("Empty");
			result = false;
		} else if( !strRegistrationCode.matches("[0-9a-zA-Z]+") || strRegistrationCode.length() != 8) {
			labelRegistrationCodeInfo.setText("Illegal");
			result = false;
		} 
		
		String strRandomKey = textRandomKey.getText().trim();
		if("".equals(strRandomKey)) {
			labelRandomKeyInfo.setText("Empty");
			result = false;
		} else if(strRandomKey.length() != 10) {
			labelRandomKeyInfo.setText("Illegal");
			result = false;
		}
		
		return result;
	}
	
	private void cleanText() {
		for(JTextField field : fields) {
			field.setText("");
		}
	}
	
	private void cleanInfo() {
		for(JLabel label : labels) {
			label.setText("");
		}
	}
}
